#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// 替换项目名称
function replaceProjectName(projectName) {
  const files = [
    'package.json',
    'README.md',
    'docker-compose.yml'
  ];

  files.forEach(file => {
    const filePath = path.join(__dirname, '..', file);
    if (fs.existsSync(filePath)) {
      let content = fs.readFileSync(filePath, 'utf8');
      content = content.replace(/nestjs-template/g, projectName);
      fs.writeFileSync(filePath, content);
    }
  });
}

// 主函数
function main() {
  const projectName = process.argv[2] || 'my-nestjs-app';
  console.log(`正在初始化项目: ${projectName}`);
  
  try {
    replaceProjectName(projectName);
    execSync('pnpm install', { stdio: 'inherit' });
    console.log('项目初始化完成!');
  } catch (error) {
    console.error('初始化失败:', error);
    process.exit(1);
  }
}

main();